<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Gift;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class GiftController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Gift(['user:id,avatar,nickname','auth:user_id,name','income:id,avatar,nickname' => ['auth:user_id,name']]), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->fixColumns(1);
            $grid->column('user.avatar','用户头像')->image('',80,80);
            $grid->column('user.nickname','用户昵称');
            $grid->column('auth.name','真实姓名');
            $grid->column('amount');
            $grid->column('type')
                 ->using([1 => '企业打赏',2 => '品牌打赏'])
                 ->label([1 => 'success',2 => 'primary']);
            $grid->column('income.avatar','收益人用户头像')->image('',80,80);
            $grid->column('income.nickname','收益人用户昵称');
            $grid->column('income.auth.name','收益人真实姓名');
            $grid->column('order_no');
            $grid->column('status')
                 ->using([0 => '未付款',1 => '已完成'])
                 ->label([0 => 'danger',1 => 'success']);
            $grid->column('pay_time');
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('user.nickname','用户昵称')
                      ->width(3);
                $filter->equal('auth.name','真实姓名')
                    ->width(3);

                $filter->equal('income.nickname','收益人用户昵称')
                    ->width(3);
                $filter->equal('income.auth.name','收益人真实姓名')
                    ->width(3);

                $filter->equal('status')
                       ->select([0 => '未付款',1 => '已完成'])
                       ->width(3);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Gift(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('amount');
            $show->field('type');
            $show->field('income_user_id');
            $show->field('order_no');
            $show->field('status');
            $show->field('pay_time');
            $show->field('created_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Gift(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('amount');
            $form->text('type');
            $form->text('income_user_id');
            $form->text('order_no');
            $form->text('status');
            $form->text('pay_time');
            $form->text('created_at');
        });
    }
}
